
import util from "/common/js/util.js";
var app = getApp();
import Qs from "qs";

const pageSize = 5;

Page({
  data: {
    categoryList:[],
    selectCategory:{},
    scrollTop: 0,
    toView: "",
    dataList: [],
    t:new Date().getTime(),
    page: 1,
    pageSize: pageSize,
    showLoadMore: false,
    showLoadMoreTxt: "over",
    totalResult: pageSize,
  },
  onLoad(e) {

    this.getIsLogin();
  },
  initFn(){
    this.getCategoryFn();
    this.tableList();
  },
  getIsLogin(){
    let api = "/user/info/getIsLogin", list = {  };
    app.$post(api, list).then(re => {
      try {
        if (re.code == 401) {
          app.authLoginFn().then(res=>{
           // console.log("--res:",res);
            this.initFn();

          })
        }
        else {
          this.initFn();
        };
      } catch (error) {
        
      }

    }).catch(err => {
      
      
    });
  },
  getCategoryFn(){
    let api = "/user/good/categoryAll";
    let list={}
    app.$post(api, list).then(res => {
    
      if (res.code == 200) {
        this.setData({
          categoryList: res.data,
        });
      }else{
        util.message(res.message);

      }
    })
  },
  handleClickItem(e){
    // let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;

    let categoryList =this.data.categoryList;
    let item = categoryList[index];
  //  item.check=!item.check;

    let category=this.data.selectCategory;

    if(util.isNotEmpty(category)){
      if(category.categoryId!=item.categoryId){
        category=item;
      }else{
        category={};
      }
    }else{
      category=item;
    }
    // if(item.check){
    //   category={};
    // }else{
    //   category=item;
    // }
    this.setData({
      selectCategory:category,
      [`categoryList[${index}]`]:item
    });
    this.searchFn();
  },
  onClickCard(e){
    let item = e.currentTarget.dataset.item;
    let json={goodId:item.goodId}
    let p = Qs.stringify(json);
    let path = '/pages/shop/shop_detail/shop_detail';
    let url = `${path}?${p}`;
    dd.navigateTo({
      url
    })
  },
  goToOrder(){
    let path = '/pages/shop/order_list/order_list';
    let url = `${path}`;
    dd.navigateTo({
      url
    })
  },
  searchFn(){
    this.setData({
      page:1,
      dataList:[]
    });
    this.tableList();
  },
  tableList(){
    let api = "/user/good/goodList";
    let list = {
      status: 1,
      page: this.data.page,
      pageSize: this.data.pageSize,
      
    };
    if(util.isNotEmpty(this.data.selectCategory.categoryId)){
      list.categoryId=this.data.selectCategory.categoryId;
    }
    this.setData({
      showLoadMoreTxt: "load"
    });
    if (this.data.dataList.length >= this.data.totalResult&&this.data.totalResult!=0) {
      this.setData({
        showLoadMoreTxt: "over"
      })
      dd.stopPullDownRefresh();
      return false;
    }
    dd.showLoading({
        content: '数据加载中...',
     })
     let dataList=this.data.dataList;
     app.$post(api, list).then(res => {
       dd.stopPullDownRefresh()
      dd.hideLoading();
       if (res.code == 200) {
 
         let arr_list=dataList.concat(res.data.list);
         arr_list.forEach(r=>{
          let img=null;
          if(r.mainImg){
           let img_arr= r.mainImg.split(",");
           img=img_arr[0];
          }
          // if(img){
          //   img+=`?t=`+new Date().getTime();
          // }
          r.img=img
         })
         let page=this.data.page;
         page=page+1;
         // console.log(JSON.stringify(arr_list[0]));
         this.setData({
           totalResult: res.data.totalResult,
           page,
           dataList: arr_list,
           showLoadMoreTxt: "over"
         });
       }else{
         util.message(res.message);
 
       }
     })

  },
  onPullDownRefresh() {
    this.searchFn();
  },
  onScrollToLower(){
    this.tableList();
  }
});
